package com.nicholas.exampleweb.util;

import com.nicholas.exampleweb.model.User;
import com.nicholas.week1.util.PropertyUtil;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DBManager {

    public static final String DROP_USERS_TABLE_DDL_SQL = "DROP TABLE users";

    public static final String CREATE_USERS_TABLE_DDL_SQL = "CREATE TABLE users(" +
            "id INT NOT NULL PRIMARY KEY, " +
            "name VARCHAR(16) NOT NULL, " +
            "password VARCHAR(64) NOT NULL, " +
            "email VARCHAR(64) NOT NULL, " +
            "phoneNumber VARCHAR(64) NOT NULL" +
            ")";



    public static Connection getConnection() {
        String jndiName = PropertyUtil.getProperty("jndi-name");
        Context context = null;
        try {
            context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup(jndiName);
            Connection connection = dataSource.getConnection();
            return connection;
        } catch (NamingException | SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
/*
    public void releaseConnection() {
        if (this.connection != null) {
            try {
                this.connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e.getCause());
            }
        }
    }*/
}
